// packageC/pages/package/gonzhuangronqiguanli/package_improve/add_improve/add_improve.js
import WxValidate from "../../../../../../Wx_Validate/WxValidate"
const mybehavior = require("../../../../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    fs_upload_url: "https://logisticsengineering.gree.com/container_manage_api/container_manage",
    baseUrl: "https://logisticsengineering.gree.com/container_manage_api/advise_manage",
    img_required: false,
    options1: [],
    choose_img_btn_disabled: false,
    data_list: [],
    company: "",
    manager: "",
    material_type: "",
    material_code: "",
    material_name: "",
    problem_description: "",
    problem_files: [],
    advice: "",
    advice_files: [],
    workshop_line: "",
    meet_standard: "",
    submit_btn: false,
    zz_show: false,


  },
  // 获取物料类别数据信息
  getMaterialnDatas() {
    let url = this.data.baseUrl + "/query_material_type"
    let data = {
      company: this.data.company
    }
    this.post(url, data).then(res => {
      console.log("获取物料类别数据信息", res.data)
      let arr = []
      for (let i in res.data) {
        arr.push(res.data[i].material_type)
      }
      if (res.code == 20000) {
        this.setData({
          options1: arr,
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      // wx.showToast({
      //   title: err.msg,
      //   icon: 'error'
      // })
    })
  },
  // 选择来料包装是否符合标准
  onChange(event) {

    this.setData({
      meet_standard: event.detail,
    });
    let self = this
    if (event.detail == "no") {
      wx.showModal({
        title: '是否进行监督举报',
        content: '',
        complete: (res) => {
          if (res.cancel) {
            self.setData({
              meet_standard: event.detail,
            })
          }
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/containers_inspection/containers_inspection',

            })
          }
        }
      })
    }
  },
  showDetail(e) {
    let row = e.currentTarget.dataset.row_data
    console.log(row);

    wx.navigateTo({
      url: '/packageB/pages/package/pfep/standard_query/supplier_modify_standard/supplier_modify_standard',
      success(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', row)

      }
    })
  },

  showDetail1(e) {
    let row = e.currentTarget.dataset.row_data
    console.log(row);

    wx.navigateTo({
      url: '/packageB/pages/package/pfep/standard_query/zzj_standard/zzj_standard',
      success(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', row)

      }
    })
  },
  // 获取--物料编码模板--数据
  getStandardDatas(e) {
    wx.showLoading({
      title: '处理中...',
    })
    let url = "https://logisticsengineering.gree.com/pfep2_api/standard_doc_store_manage/transfer_batch2material_num2"
    let data = {
      batch_num: e,
      use_in: this.data.company,
    }
    console.log("传过去的数据", data);
    this.post(url, data).then(res => {
      console.log("物料编码模板", res.data.results);

      if (res.code == 20000) {
        if (res.data.material_type != "自制件") {
          if (res.data.str_type == "物料编码") {
            this.setData({
              material_code: res.data.raw_str,
            })
          } else if (res.data.str_type == "物料标识卡") {
            this.setData({
              material_code: res.data.raw_str,
            })
          } else if (res.data.str_type == "物料批次号") {
            this.setData({
              material_code: res.data.raw_str,
            })
          }
          if (res.data.results.length == 0 && res.data.str_type != "" && res.data.material_num != "") {
            wx.showToast({
              title: '该物料编码无标准',
              icon: 'none'
            })
          }
          if (res.data.results.length == 0 && res.data.str_type == "物料批次号" && res.data.material_num == "") {
            wx.showToast({
              title: '该物料批次号无物料编码信息',
              icon: 'none'
            })
          }
          if (res.data.results.length == 0 && res.data.str_type == "物料标识卡" && res.data.material_num == "") {
            wx.showToast({
              title: '该物料标识卡无物料编码信息',
              icon: 'none'
            })
          }
          if (res.data.results.length == 0 && res.data.str_type == "" && res.data.material_num == "") {
            wx.showToast({
              title: '该编码不存在',
              icon: 'none'
            })
          }

          this.setData({
            zz_show: false,
            data_list: res.data.results,
            material_code: res.data.material_num
          })
          this.getMaterialName()
          if (res.data.results.length != 0) {
            wx.hideLoading()

          }
        } else {
          this.setData({
            zz_show: true,
            data_list: res.data.results,
            material_code: res.data.material_num

          })

        }
        this.getMaterialName()
        if (res.data.results.length != 0) {
          wx.hideLoading()

        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 按钮查询获取--物料编码模板--数据
  getStandardDatas1() {
    wx.showLoading({
      title: '处理中...',
    })
    let url = "https://logisticsengineering.gree.com/pfep2_api/standard_doc_store_manage/transfer_batch2material_num2"
    let data = {
      batch_num: this.data.material_code,
      use_in: this.data.company,
    }
    console.log("传过去的数据", data);
    this.post(url, data).then(res => {
      console.log("物料编码模板", res.data.results);

      if (res.code == 20000) {

        if (res.data.material_type != "自制件") {
          if (res.data.str_type == "物料编码") {
            this.setData({
              material_code: res.data.raw_str,
            })
          } else if (res.data.str_type == "物料标识卡") {
            this.setData({
              material_code: res.data.raw_str,
            })
          } else if (res.data.str_type == "物料批次号") {
            .0
            this.setData({
              material_code: res.data.raw_str,
            })
          }
          if (res.data.results.length == 0 && res.data.str_type != "" && res.data.material_num != "") {
            wx.showToast({
              title: '该物料编码无标准',
              icon: 'none'
            })
          }
          if (res.data.results.length == 0 && res.data.str_type == "物料批次号" && res.data.material_num == "") {
            wx.showToast({
              title: '该物料批次号无物料编码信息',
              icon: 'none'
            })
          }
          if (res.data.results.length == 0 && res.data.str_type == "物料标识卡" && res.data.material_num == "") {
            wx.showToast({
              title: '该物料标识卡无物料编码信息',
              icon: 'none'
            })
          }
          if (res.data.results.length == 0 && res.data.str_type == "" && res.data.material_num == "") {
            wx.showToast({
              title: '该编码不存在',
              icon: 'none'
            })
          }

          this.setData({
            zz_show: false,
            data_list: res.data.results,
            material_code: res.data.material_num
          })
          this.getMaterialName()
          if (res.data.results.length != 0) {
            wx.hideLoading()

          }
        } else {
          this.setData({
            zz_show: true,
            data_list: res.data.results,
            material_code: res.data.material_num

          })
          this.getMaterialName()
          console.log("自制件", res.data);
          if (res.data.results.length != 0) {
            wx.hideLoading()

          }
        }



      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },





  // 获取--物料编码模板--数据
  // getStandardDatas() {
  //   let query_cond = {
  //     "content.infoData.material_code": this.data.material_code,
  //     use_in: this.data.company
  //   }
  //   let url = "https://logisticsengineering.gree.com/pfep2_api/standard_doc_store_manage/query_standard_doc_store"
  //   let data = {
  //     page: 1,
  //     page_size: 99999,
  //     query_cond: query_cond,
  //     tb_key: ""
  //   }
  //   this.post(url, data).then(res => {
  //     console.log("物料编码模板", res.data.results);

  //     if (res.code == 20000) {
  //       this.getMaterialName()

  //       this.setData({
  //         data_list: res.data.results,


  //       })

  //     } else {
  //       wx.showToast({
  //         title: res.msg,
  //         icon: 'error'
  //       })
  //     }
  //   }).catch(err => {
  //     console.log(err);
  //     wx.showToast({
  //       title: err.msg,
  //       icon: 'none'
  //     })
  //   })
  // },

  // 获取--物料编码获取名称--数据
  getMaterialName() {

    let url = this.data.baseUrl + "/query_material_info"
    let data = {
      material_num: this.data.material_code,
    }
    this.post(url, data).then(res => {

      if (res.code == 20000) {

        this.setData({
          material_name: res.data.dsca,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },

  // 扫描
  scan1(e) {
    let key = e.currentTarget.dataset.key
    console.log(key);
    const self = this;
    wx.scanCode().then((res) => {
      const label_code = res.result;
      wx.showLoading({
        title: "loading"
      })
      console.log(label_code);
      setTimeout(function () {
        wx.hideLoading()
        self.setData({
          [key]: label_code,
        })
        self.getStandardDatas(label_code)

      }, 500)

    }).catch(err => {
      console.log(err);
    });
  },
  // 下拉选择
  getPickerVlaue(e) {
    let key = e.target.id
    this.setData({
      [key]: e.detail

    })
  },
  // 上传照片
  getUploaderVlaue(e) {
    let key = e.target.id
    this.setData({
      [key]: e.detail
    })
  },
  // 提交
  submit() {
    this.setData({
      submit_btn: true,
    })
    let url = this.data.baseUrl + "/advice_submit"
    let data = {
      material_type: this.data.material_type,
      material_code: this.data.material_code,
      material_name: this.data.material_name,
      problem_description: this.data.problem_description,
      problem_files: this.data.problem_files,
      advice: this.data.advice,
      advice_files: this.data.advice_files,
      workshop_line: this.data.workshop_line,
      meet_standard: this.data.meet_standard,
      user_id: wx.getStorageSync('_id'),
    }
    console.log(data);
    if (!this.WxValidate.checkForm(data)) {
      const error = this.WxValidate.errorList[0]
      this.setData({
        submit_btn: false,
      })
      wx.showToast({
        title: error.msg,
        icon: 'none'
      })
    } else {
      this.post(url, data).then(res => {

        if (res.code == 20000) {
          this.setData({
            submit_btn: false,
          })
          wx.showToast({
            title: res.msg,
            icon: 'success'
          })
          this.reset()
        } else {
          this.setData({
            submit_btn: false,
          })
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        this.setData({
          submit_btn: false,
        })
        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
    }
  },
  // 重置表单
  reset() {
    this.setData({
      material_type: "",
      material_code: "",
      material_name: "",
      problem_description: "",
      problem_files: [],
      advice: "",
      advice_files: [],
      workshop_line: "",
      meet_standard: "",
      data_list: [],
    })
  },
  //初始化表单验证规则
  initValidate() {
    let rules = {
      material_type: {
        required: true,
      },
      material_code: {
        required: true,
      },
      material_name: {
        required: true,
      },
      problem_description: {
        required: true,
      },

      problem_files: {
        required: true,
      },
      advice: {
        required: true,
      },





    }

    let message = {
      material_type: {
        required: "物料类-不能为空",
      },
      material_code: {
        required: "物料编码-不能为空",
      },
      material_name: {
        required: "物料名称-不能为空",
      },
      problem_description: {
        required: "问题描述-不能为空",
      },

      problem_files: {
        required: "现状照片-不能为空",
      },
      advice: {
        required: "改善建议-不能为空",
      },


    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.initValidate()
    this.setData({
      company: wx.getStorageSync('company'),
      manager: wx.getStorageSync('department'),
    })
    this.getMaterialnDatas()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})